package com.yixun.controller;

import com.yixun.entity.YxMenu;
import com.yixun.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;


    @RequestMapping(value = "/list")
    public String list(){
        return "org/menu/menuList";
    }

    /**
     * 获取菜单树
     * @param menu_id
     * @return
     */
    @RequestMapping(value = "/getMenuTreeJsonStr/{menu_id}")
    @ResponseBody
    public String getMenuTreeJsonStr(@PathVariable(value = "menu_id") String menu_id){
        return menuService.getMenuTree(menu_id);
    }

    /**
     * 获取子菜单列表
     * @param menu_id
     * @return
     */
    @RequestMapping(value = "/getChildMenuList/{menu_id}")
    @ResponseBody
    public List<YxMenu> getChildMenuList(@PathVariable(value = "menu_id") String menu_id){
        return menuService.getChildMenuList(menu_id);
    }

    /**
     * go 菜单编辑页面
     * @param parentID
     * @param menu_id
     * @param model
     * @return
     */
    @RequestMapping(value = "go_menu_add")
    public String goMenuAdd(String parentID, String menu_id, Model model){

        model.addAttribute("parentID", parentID);

        if (null != menu_id && menu_id != "") {
            model.addAttribute("menu_id", menu_id);
        } else {
            model.addAttribute("menu_id", "null");
        }
        return "org/menu/menu_add";
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody YxMenu menu) {
        int row = menuService.insertMenu(this.getSessionUser(), menu);
        if (row == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改时 信息回显
     * @param menu_id
     * @return
     */
    @RequestMapping(value = "/getMenuBean/{menu_id}")
    @ResponseBody
    public YxMenu getMenuBean(@PathVariable(value = "menu_id") String menu_id){
        return menuService.getMenuBean(menu_id);
    }


    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody YxMenu menu){
        int row = menuService.updateMenu(this.getSessionUser(), menu);
        if (row == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public boolean del(String id){
        int row = menuService.delMenu(this.getSessionUser(), id);
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 跳向select_single_menu
     * @param handl_name
     * @param model
     * @return
     */
    @RequestMapping(value = "/select_single_menu")
    public String selectSingleMenu(String handl_name, Model model){
        model.addAttribute("handl_name",handl_name);
        return "org/menu/select_single_menu";
    }

    /**
     * 移动菜单
     * @param parent_id
     * @param menu_ids
     * @return
     */
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    @ResponseBody
    public boolean move(String parent_id, String menu_ids){
        int row = menuService.moveMenu(this.getSessionUser(), parent_id, menu_ids);
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 保存 Menu 排序
     * @param map
     * @return
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ResponseBody
    public boolean sort(@RequestBody Map<String, String> map){
        String id = map.get("id");
        int row = menuService.sortMenu(this.getSessionUser(), id);
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }

}
